import wxpyview.ui.core.dataprovider.common.library

def getData(dom_element):
    return _object2array(dom_element)

def _object2array(dom_element):
    data = []
    
    if dom_element.nodeType != dom_element.ELEMENT_NODE:
        raise IOException
    
    if dom_element.nodeName != "wx:TreeCollection":
        raise IOException
    
    for child_dom in dom_element.childNodes:
        if child_dom.nodeType != child_dom.ELEMENT_NODE: continue
        
        if child_dom.nodeName == "wx:Object":
            buffer = _dfs(child_dom)
            data.append(buffer)
            
    return data[0]

def _dfs(dom_element):
    buffer = []
    
    child_doms = dom_element.childNodes
    child_doms = filter(lambda x: x.nodeType == x.ELEMENT_NODE, child_doms)
    
    if child_doms == []:
        return wxpyview.ui.core.dataprovider.common.library.object2hash(dom_element)
    else:
        tree_branch = wxpyview.ui.core.dataprovider.common.library.object2hash(dom_element)

    for child_dom in child_doms:
        data = _dfs(child_dom)
        buffer.append(data)
        
    return [tree_branch, buffer]